package com.ruoyi.mongo.cet4.service.impl;

import com.ruoyi.mongo.cet4.service.PrimaryInterface;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mongo.service.MongoConstants;
import com.ruoyi.mongo.service.MongoPrimaryService;
import com.ruoyi.mongo.word.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PrimaryService implements PrimaryInterface {

    private final MongoPrimaryService mongoService;

    public PrimaryService(MongoPrimaryService mongoService) {
        this.mongoService = mongoService;
    }

    @Override
    public List<WordPrimaryEntity> getListData(String unit, String book) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return mongoService.getPrimaryData(unit, book, userId);
    }

    @Override
    public List<WordPrimaryEntity> getListData(String unit, String book, int times) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return mongoService.getPrimaryData(unit, book, times, userId);
    }

    @Override
    public void saveStudyData(WordStudyEntity entity) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_STUDY + "_" + userId;
        mongoService.savePrimaryStudyData(entity, collection);
    }

    @Override
    public List<WordStudyEntity> getStudyData(String unit, String book) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return getStudyData(unit, book, userId);
    }

    @Override
    public List<WordStudyEntity> getStudyData(String unit, String book, Long userId) {
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_STUDY + "_" + userId;
        return mongoService.getPrimaryStudyData(unit, book, collection);
    }

    @Override
    public List<WordStudyEntity> getStudyData(String practice, Long userId) {
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_STUDY + "_" + userId;
        return mongoService.getPrimaryStudyData(practice,  collection);
    }


    @Override
    public void saveTestData(WordTestEntity entity) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_TEST + "_" + userId;
        mongoService.savePrimaryTestData(entity, collection);
    }

    @Override
    public void saveTestData(List<WordTestEntity> list) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_TEST + "_" + userId;
        mongoService.savePrimaryTestData(list, collection);
    }

    @Override
    public void saveTestData(WordSubmitEntity entity) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_TEST + "_" + userId;
        entity.getData().forEach(data -> {
            data.setBook(entity.getBook());
            data.setUnit(entity.getUnit());
            if (data.getChinese() != null && data.getSelected() != null) {
                data.setResult(data.getChinese().equals(data.getSelected()));
            }
            mongoService.savePrimaryTestData(data, collection);
        });
    }

    @Override
    public List<WordTestEntity> getTestData(String unit, String book) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_TEST + "_" + userId;
        return mongoService.getPrimaryTestData(unit, book, collection);
    }

    @Override
    public List<WordCheckNewRecord> getTestDetail(String practiceId) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_TEST + "_" + userId;
        return mongoService.getPrimaryTestDetail(practiceId, collection);
    }

    @Override
    public List<?> getTestDetail(String practiceId, int way) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return mongoService.getPrimaryTestDataNew(practiceId, userId);
    }

    @Override
    public List<?> getTestDetail(String practiceId, Long user) {
        return mongoService.getPrimaryTestDetail(practiceId, user);
    }

    @Override
    public void saveWriteData(WordChinToEngEntity entity) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_WRITE + "_" + userId;
        entity.set_id(null);
        entity.setTime(DateUtils.getLocalTime());
        mongoService.savePrimaryWriteData(entity, collection);
    }

    @Override
    public List<WordChinToEngEntity> getWriteData(String practiceId) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = MongoConstants.COLLECT_WORD_PRIMARY_WRITE + "_" + userId;
        return mongoService.getPrimaryWriteData(practiceId, collection);
    }
}
